package org.atlantis.grosbeak.cable.template;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用来渲染一个JSP页面的模板。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class JspTemplate implements Template {

	private String path;
	private Map<String, Object> mails;

	public JspTemplate(String path, Map<String, Object> mails) {
		this.path = path;
		this.mails = mails;
	}

	public JspTemplate(String path, Object... beans) {
		this.path = path;
		Map<String, Object> map = new HashMap<String, Object>();
		if (beans != null) {
			for (Object bean : beans) {
				map.put(bean.getClass().getSimpleName().toLowerCase(), bean);
			}
		}
		this.mails = map;
	}

	public void render(ServletContext sc, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (mails != null)
			for (Map.Entry<String, Object> entry : mails.entrySet()) {
				request.setAttribute(entry.getKey(), entry.getValue());
			}
		request.getRequestDispatcher(path).forward(request, response);
	}
}
